package com.shujia.spark

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

object Demo14Action {

  def main(args: Array[String]): Unit = {

    /**
      *
      * 操作算子会触发任务执行.每一个操作算子都会触发一个任务
      *
      */

    val conf: SparkConf = new SparkConf()
      .setAppName("make")
      .setMaster("local")


    val sc = new SparkContext(conf)

    val studentRDD: RDD[String] = sc.textFile("spark/data/students.txt")


    //1 foreach
    studentRDD.foreach(println)


    //2  count
    val length: Long = studentRDD.count()
    println(length)


    // 3 reduce
    val str: String = studentRDD.reduce((x, y) => x + "|||" + y)
    println(str)


    //4 save
    studentRDD.saveAsTextFile("spark/data/out1")


    /**
      * 5\ collect:将rdd转换成数组
      *
      * rdd中实际上是没有数据的,  转换成数据实际上是将数据拉取到内存中
      *
      */

    val arr: Array[String] = studentRDD.collect()


    arr.foreach(println)


    while (true) {

    }


  }

}
